package 并发编程15.线程池;

import java.util.concurrent.*;

/**
 * @author idea
 * @data 2019/2/6
 */
class MyRunnable implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + ":" + i);
        }
    }
}

public class ThreadPoolTest {


    public static ExecutorService newFixedThreadPool(int number) {
        //线程池最少个数，线程池最多个数，线程保持在线的一个时间长度，时间单位，线程任务的工作队列
        /**
         * @param corePoolSize the number of threads to keep in the pool, even
         *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
         * @param maximumPoolSize the maximum number of threads to allow in the
         *        pool
         * @param keepAliveTime when the number of threads is greater than
         *        the core, this is the maximum time that excess idle threads
         *        will wait for new tasks before terminating.
         * @param unit the time unit for the {@code keepAliveTime} argument
         * @param workQueue the queue to use for holding tasks before they are
         *        executed.  This queue will hold only the {@code Runnable}
         *        tasks submitted by the {@code execute} method.
         **/
        return new ThreadPoolExecutor(number, number, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
    }



    public static void main(String[] args) {
        System.out.println("原来的几种创建金线程池的方式");
        Executors.newCachedThreadPool();
        Executors.newFixedThreadPool(1);
        Executors.newScheduledThreadPool(10);
        ExecutorService pool = new ScheduledThreadPoolExecutor(10);
        //submit之后可以执行runnable或者callable对象代表线程
//        pool.submit(new MyRunnable());
        pool.execute(new MyRunnable());
        System.out.println("this ");
//        pool.submit(new MyRunnable());
//        pool.submit(new MyRunnable());
        pool.shutdown();
    }


}
